function cost = costFunction(quality, correctQuality, weights, lambda)

m = size(quality, 2);
numLayers = length(weights);

delta = (quality - correctQuality) .^ 2;
delta = sum(delta, 2);

weightsFactor = 0;

if abs(lambda) > 1e-9
    for i = 1 : numLayers
        weights{i}(:, 1) = ones(size(weights{i}(:, 1)));
    
        weightsFactor = weightsFactor + sum(sum(weights{i}.^2));
    end
end

cost = (delta + lambda * weightsFactor) / (2 * m);
